package com.hitqz.robot.biz.flow.component.oilSample;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.enums.TicketsFormFieldEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.FetchOilDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/10/4
 */
@LiteflowComponent(value = "StartFetchOilComponent",name = "[取油样机器人]开始取油")
@RequiredArgsConstructor
@Slf4j
public class StartFetchOilComponent extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;


    @Override
    public void process() throws Exception {
        // 下发开始取油
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String timesArray = String.valueOf(ticketsDto.getModalData().get("times"));
        String[] sn = timesArray.split(",");
        for (String i : sn) {
            log.info("准备开始取油:" + i);
            int times = Integer.parseInt(i);
            if (times == 0) {
                continue;
            }
            List<RobotEquipment> oils = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                    //.eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                    .eq(RobotEquipment::getType, EquipmentEnum.CAR_PLC.getType())
                    .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
                    .eq(RobotEquipment::getIsRecordParam, 1));
            log.info("取油电机编号:{}", times);
            for (RobotEquipment oil : CollUtil.emptyIfNull(oils)) {
                // 车上装开始对接
                IEquipmentService service =  equipmentFactory.getServiceByBrand(oil.getBrand());
                QueryParamsDto queryParamsDto = new QueryParamsDto();
                queryParamsDto.setPassword(oil.getPassword());
                queryParamsDto.setUsername(oil.getUsername());
                queryParamsDto.setPort(oil.getPort());
                queryParamsDto.setChannel(oil.getChannel());
                queryParamsDto.setIp(oil.getIp());
                FetchOilDto fetchOilDto = new FetchOilDto();
                fetchOilDto.setSn(times);
                service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.FET_OIL, fetchOilDto);
                log.info("上装打开取油柜");
            }
            String deviceKey = "";
            for (String key : ticketsDto.getModalData().keySet()) {
                if (key.contains(TicketsFormFieldEnum.EQUIPMENT_LIST.getFieldName())) {
                    deviceKey = key;
                    break;
                }
            }
            String device = String.valueOf(ticketsDto.getModalData().get(deviceKey));
            RobotEquipment robotEquipment = robotEquipmentService.getById(Integer.valueOf(device));
            IEquipmentService service =  equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(robotEquipment.getPassword());
            queryParamsDto.setUsername(robotEquipment.getUsername());
            queryParamsDto.setPort(robotEquipment.getPort());
            queryParamsDto.setChannel(robotEquipment.getChannel());
            queryParamsDto.setIp(robotEquipment.getIp());
            FetchOilDto fetchOilDto = new FetchOilDto();
            fetchOilDto.setSn(times);
            service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.FET_OIL, fetchOilDto);


            for (RobotEquipment oil : CollUtil.emptyIfNull(oils)) {
                // 取油完成
                IEquipmentService oilService = equipmentFactory.getServiceByBrand(oil.getBrand());
                queryParamsDto = new QueryParamsDto();
                queryParamsDto.setPassword(oil.getPassword());
                queryParamsDto.setUsername(oil.getUsername());
                queryParamsDto.setPort(oil.getPort());
                queryParamsDto.setChannel(oil.getChannel());
                queryParamsDto.setIp(oil.getIp());
                while (true) {
                    List<EquipmentParamsDto> equipmentParamsDtos = oilService.getEquipmentParams(queryParamsDto);
                    Optional<EquipmentParamsDto> fetFinishStatus = equipmentParamsDtos.stream().filter(t-> t.getKey().equals("fetStatus")).findFirst();
                    if (fetFinishStatus.isPresent()) {
                        if (fetFinishStatus.get().getValue().equals("1")) {
                            log.info("取油完成");
                            break;
                        }
                    }
                }
            }

            // 取油柜子
            //IEquipmentService oilService =  equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
            //queryParamsDto = new QueryParamsDto();
            //queryParamsDto.setPassword(robotEquipment.getPassword());
            //queryParamsDto.setUsername(robotEquipment.getUsername());
            //queryParamsDto.setPort(robotEquipment.getPort());
            //queryParamsDto.setChannel(robotEquipment.getChannel());
            //queryParamsDto.setIp(robotEquipment.getIp());
            //fetchOilDto = new FetchOilDto();
            //fetchOilDto.setSn(0);
            //oilService.executeCmd(queryParamsDto , false , EquipmentCmdEnum.FET_OIL, fetchOilDto);

        }

    }
}
